package com.softeem.score.manager.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softeem.score.manager.mapper.CourseMapper;
import com.softeem.score.manager.mapper.GradeMapper;
import com.softeem.score.manager.mapper.StudentMapper;
import com.softeem.score.manager.service.CourseService;
import com.softeem.score.model.dto.CourseDto;
import com.softeem.score.model.dto.GradeComponentDto;
import com.softeem.score.model.enmu.CourseBelongingEnum;
import com.softeem.score.model.enmu.CourseCategoryEnum;
import com.softeem.score.model.enmu.CourseNatureEnum;
import com.softeem.score.model.entity.school.Student;
import com.softeem.score.model.vo.educational.CourseVo;
import com.softeem.score.model.vo.educational.GradeCompontWeightVo;
import com.softeem.score.model.vo.educational.SchedulesVo;
import com.softeem.score.model.vo.school.MapVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2024-02-14
 */
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private GradeMapper gradeMapper;

    @Override
    public PageInfo<CourseVo> findByPage(CourseDto courseDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        // 先查询课程列表
        List<CourseVo> courseVoList = courseMapper.findByPage(courseDto);
        // 遍历课程列表，并转换为CourseVo对象
        for (CourseVo courseVo : courseVoList) {
            courseVo.setTeacherIds(courseMapper.selectTeacherIdsByCourseId(courseVo.getId()));
            courseVo.setCourseTeacher(courseMapper.selectCourseTeacherByCourseId(courseVo.getId()));
            courseVo.setCategoryName(CourseCategoryEnum.getById(courseVo.getCategory()).getValue());
            courseVo.setNatureName(CourseNatureEnum.getById(courseVo.getNature()).getValue());
            courseVo.setOwnershipName(CourseBelongingEnum.getById(courseVo.getOwnership()).getValue());
            List<Long> gradeComponentIds = gradeMapper.findGradeComponentsByCourseId(courseVo.getId());
            courseVo.setGradeComponentsIds(gradeComponentIds);
        }
        return new PageInfo<>(courseVoList);
    }

    @Override
    public List<MapVo> findAllCourse() {
        return courseMapper.findAll();
    }

    @Override
    public List<MapVo> findCourseById(List<Long> courseIdList) {
        return courseMapper.findCourseById(courseIdList);
    }

    @Transactional
    @Override
    public void saveCourse(CourseVo courseVo) {
        courseMapper.saveCourse(courseVo); // 保存课程信息
        // 获取插入后生成的课程id
        Long courseId = courseVo.getId();
        // 保存课程教师信息
        courseMapper.saveCourseTeacher(courseId, courseVo.getTeacherIds());
        // 保存成绩计算项信息
        gradeMapper.doAssign(courseId, courseVo.getGradeComponentsIds());
    }

    @Transactional
    @Override
    public boolean deleteById(Long courseId) {
        if (courseMapper.selectTeacherIdsByCourseId(courseId) != null) {
            courseMapper.deleteCourseTeacher(courseId);
        }
        return courseMapper.deleteById(courseId) > 0;
    }

    @Transactional
    @Override
    public void updateCourse(CourseVo courseVo) {
        //先删除之前绑定的教师信息
        courseMapper.deleteCourseTeacher(courseVo.getId());
        //如果没有绑定就先保存
        if (courseVo.getTeacherIds() != null) {
            courseMapper.saveCourseTeacher(courseVo.getId(), courseVo.getTeacherIds());
        }
        //如果成绩计算项有变化就先删除
        List<Long> gradeComponentsByCourseId = gradeMapper.findGradeComponentsByCourseId(courseVo.getId());
        if (!gradeComponentsByCourseId.equals(courseVo.getGradeComponentsIds())) {
            gradeMapper.deleteGradeComponentsWeightBycourseId(courseVo.getId());
            if (courseVo.getGradeComponentsIds() != null) {
                gradeMapper.doAssign(courseVo.getId(), courseVo.getGradeComponentsIds());
            }
        }
        courseMapper.updateCourse(courseVo);
    }

    @Override
    public List<MapVo> findAllGradeComponents() {
        return courseMapper.selectAllGradeComponent();
    }

    @Override
    public List<GradeCompontWeightVo> selectGradeComponentByCourseId(Long courseId) {
        return courseMapper.selectGradeComponentByCourseId(courseId);
    }

    @Override
    public void updateGradeComponentsWeight(GradeComponentDto gradeComponentDto) {
        List<GradeComponentDto.GradeComponent> gradeComponentList = gradeComponentDto.getGradeComponentList();
        for (GradeComponentDto.GradeComponent gradeComponent : gradeComponentList) {
            gradeMapper.updateGradeComponentWeight(gradeComponentDto.getCourseId(), gradeComponent);
        }
    }

    @Override
    public List<MapVo> findByCollegeId(Long collegeId, Integer category, Integer nature) {
        return courseMapper.findALlByCollegeId(collegeId, category, nature);
    }

    @Override
    public List<MapVo> findCourseByCollegeId(Long collegeId) {
        return courseMapper.findALlByCollegeId(collegeId, null, null);
    }

    @Override
    public List<SchedulesVo> findSchedulesByclassId(Long classesId) {
        List<SchedulesVo> schedulesVoList = courseMapper.findSchedulesVoByClassId(classesId); //得到所有的课程
        if (schedulesVoList != null) {
            for (SchedulesVo schedulesVo : schedulesVoList) {
                schedulesVo.setCourseCategoryName(CourseCategoryEnum.getById(schedulesVo.getCourseCategory()).getValue());
                schedulesVo.setCourseNatureName(CourseNatureEnum.getById(schedulesVo.getCourseNature()).getValue());
                schedulesVo.setCourseOwnershipName(CourseBelongingEnum.getById(schedulesVo.getCourseOwnership()).getValue());
            }
        }
        return schedulesVoList;
    }

    @Override
    public List<SchedulesVo> findSchedulesByStudentNumber(String studentNumber) {
        Student student = studentMapper.findByStudentNumber(studentNumber);
        return findSchedulesByclassId(student.getClassesId());
    }

    @Override
    public List<MapVo> findTeacherByCourseId(Long courseId) {
        return courseMapper.getTeachersByCourseId(courseId);
    }

    @Override
    public List<CourseVo> FindAllTeacherCoursesByTeacherCode(String teacherCode) {
        List<CourseVo> courseVoList = courseMapper.findCourseInfoByTeacherCode(teacherCode);
        if (courseVoList != null) {
            for (CourseVo courseVo : courseVoList) {
                courseVo.setCategoryName(CourseCategoryEnum.getById(courseVo.getCategory()).getValue());
                courseVo.setNatureName(CourseNatureEnum.getById(courseVo.getNature()).getValue());
                courseVo.setOwnershipName(CourseBelongingEnum.getById(courseVo.getOwnership()).getValue());
            }
        }
        return courseVoList;
    }

}
